package paproject.task;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import paproject.bean.Mv;
import paproject.httputil.HttpUtils;
import paproject.service.MvService;

@Component
public class ItemTask {

    @Autowired
    private HttpUtils httpUtils;

    @Autowired
    private MvService mvService;

    //当下载任务完成后，间隔100秒就行下一次的任务
    @Scheduled(fixedDelay = 100*1000)
    public void itemTask() throws Exception{
        //声明需要解析的初试地址：https://m.fdy.cc/dongman/index.html
        String url = "http://m.fdy.cc/dongman/index";

        //按照页码就行解析遍历
        for(int i = 2 ; i <= 3 ; i++){
            String html = httpUtils.doGetHtml("url+i+.+html");
            //解析页面，获取图片数据并存储
            this.parse(html);
        }

    }

    //解析页面
    private void parse(String html) throws Exception{
        //解析html获取Document
        Document document = Jsoup.parse(html);

        Elements elements = document.select("div.list.mb > ul > li");

        for (Element element : elements){
            //Mv mv = new Mv();
            String title = element.select("a > img").attr("alt");
            //mv.setTitle(title);
            String picurl = element.select("a > img").attr("src");
            System.out.println(picurl);
            String picName = this.httpUtils.doGetPic(picurl);
            //mv.setPic(picName);
        }

    }
}
